package com.hdu.mockService.handler;

import com.hdu.mockService.annotation.RandomBoolean;
import org.apache.commons.collections4.CollectionUtils;

import java.lang.reflect.Field;
import java.util.List;

import static com.hdu.mockService.utils.ReflectionUtils.getFieldsByTargetAnnotation;

public class BooleanMockHandler implements IMockHandler {
    @Override
    public void partitionMock(Object mockObj) {
        List<Field> booleanMockFields = getFieldsByTargetAnnotation(mockObj.getClass(), RandomBoolean.class);
        if (CollectionUtils.isEmpty(booleanMockFields)) {
            return;
        }
        for (Field field : booleanMockFields) {
            field.setAccessible(true);
            try {
                field.set(mockObj, getRandomBoolean());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    protected boolean getRandomBoolean() {
        return Math.random() > 0.5;
    }
}
